import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Time:22:03
 */
public class demo1 {
    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] check;
    public List<List<Integer>> permuteUnique(int[] nums) {
        path=new ArrayList<>();
        ret =new ArrayList<>();
        check =new boolean[nums.length];

        Arrays.sort(nums);
        dfs(nums,0);
        return ret;
    }

    public void dfs(int[] nums,int pos){
        //递归出口
        if(pos==nums.length){
            ret.add(new ArrayList<>(path));
            return;
        }

        //合法
        for(int i=0;i<nums.length;i++){
            //剪枝
            if(check[i]==false&&(i==0 || nums[i]!=nums[i-1] || check[i-1]==true)){
                path.add(nums[i]);
                check[i]=true;
                dfs(nums,pos+1);//递归下一个位置
                //恢复现场
                path.remove(path.size()-1);
                check[i]=false;
            }
        }
    }
}
